<?php
class Orderout extends AdminController {
    public function __construct() {
        parent::__construct();
    }
    
    public function index() {
        $this->_data["loadPage"]="orderout/index_view";
        $this->_data["titlePage"]="Quản lý phiếu xuất";
        $this->_data["mess_succ"]=$this->session->flashdata("flash_mess");
        
        $this->session->unset_userdata("sess_search_username");
        $this->session->unset_userdata("sess_search_datefrom");
        $this->session->unset_userdata("sess_search_dateto");
        $this->session->unset_userdata("sess_search_pricefrom");
        $this->session->unset_userdata("sess_search_priceto");
        $this->session->unset_userdata("sess_search_status");
        
        $this->load->model("morderout");
        $this->load->library("pagination");
        $config=array(
            "base_url"=>base_url()."admin_combo/orderout/index/",
            "total_rows"=>$this->morderout->countAll(),
            "per_page"=>$this->_data["perpage"],
            "uri_segment"=>4
        );
        $start=$this->uri->segment(4)==""?0:$this->uri->segment(4);
        $this->pagination->initialize($config);
        if($this->morderout->listAll($config["per_page"], $start) == false)
            $this->_data["info"] = "empty";
        else
            $this->_data["info"]=$this->morderout->listAll($config["per_page"], $start);
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function search() {        
        $this->_data["loadPage"]="orderout/search_view";
        $this->_data["titlePage"]="Quản lý phiếu xuất";
        
        $username=$this->session->userdata("sess_search_username");
        $datefrom=$this->session->userdata("sess_search_datefrom");
        $dateto=$this->session->userdata("sess_search_dateto");
        $pricefrom=$this->session->userdata("sess_search_pricefrom");
        $priceto=$this->session->userdata("sess_search_priceto");
        $status=$this->session->userdata("sess_search_status");
        
        $this->load->helper("status_helper");
        $this->load->model("mstatus");
        if($this->mstatus->listAll()!=false)
            $this->_data["info_status"]=$this->mstatus->listAll();
        
        $this->load->model("morderout");
        $this->load->library("pagination");
        $config=array(
            "base_url"=>base_url()."admin_combo/orderout/search/",
            "total_rows"=>$this->morderout->countAllResults($username, $datefrom, $dateto, $pricefrom, $priceto, $status)["count"],
            "per_page"=>$this->_data["perpage"],
            "uri_segment"=>4
        );
        $start=$this->uri->segment(4)==""?0:$this->uri->segment(4);
        $this->pagination->initialize($config);
        if($this->morderout->search($username, $datefrom, $dateto, $pricefrom, $priceto, $status, $config["per_page"], $start) == false)
            $this->_data["info"] = "empty";
        else
            $this->_data["info"]=$this->morderout->search($username, $datefrom, $dateto, $pricefrom, $priceto, $status, $config["per_page"], $start);
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function searchdata() {
        if(isset($_POST["btnSearchOrderout"])) {
            $sess=array(
                "sess_search_username"=>$this->input->post("txtUsername"),
                "sess_search_datefrom"=>$this->input->post("txtDateFrom"),
                "sess_search_dateto"=>$this->input->post("txtDateTo"),
                "sess_search_pricefrom"=>$this->input->post("txtFrom"),
                "sess_search_priceto"=>$this->input->post("txtTo"),
                "sess_search_status"=>$this->input->post("slStatus")
            );
            $this->session->set_userdata($sess);
            redirect(base_url()."admin_combo/orderout/search");
        }
    }
    
    public function edit() {
        $this->_data["loadPage"]="orderout/edit_view";
        $this->_data["titlePage"]="Chỉnh sửa phiếu xuất";
        
        //load data
        $this->load->helper("status_helper");
        $this->load->model("mstatus");
        if($this->mstatus->listAll()!=false)
            $this->_data["info_status"]=$this->mstatus->listAll();
        
        $this->load->model("morderout");
        $id=$this->uri->segment(4);
        $tmp=$this->morderout->getById($id);
        if($tmp!=false)
            $this->_data["info_edit"]=$tmp;
        
        if($this->input->post("ok")) {
            $data=array(
                "oo_note"=>$this->input->post("txtNote"),
                "status_id"=>$this->input->post("slStatus")
            );
            $this->morderout->update($data, $id);
            $this->session->set_flashdata("flash_mess", "Thêm level thành công.");
            redirect(base_url()."admin_combo/orderout/");
        }
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function del() {
        $id=$this->uri->segment(4);
        $this->load->model("morderout");
        $this->morderout->del($id);
        $this->session->set_flashdata("flash_mess", "Xóa phiếu xuất thành công.");
        redirect(base_url()."admin_combo/orderout/");
    }
}
?>
